package com.ewanghuiju.app.presenter.resale;

import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.resale.ResaleHomeContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.ResaleShopResponBean;
import com.ewanghuiju.app.model.bean.response.WaterGoodsClassifyBean;
import com.ewanghuiju.app.model.bean.response.WaterShopResponBean;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class 转售商品列表页
 * @time 2020/2/16
 */
public class ResaleHomePresenter extends RxPresenter<ResaleHomeContract.View> implements ResaleHomeContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public ResaleHomePresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData(int page, String id) {
        addSubscribe(mDataManager.getJfgoodscate(page, id)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<WaterShopResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<WaterShopResponBean>> optional) {
                        //TODO
                        List<WaterShopResponBean> tempList =  optional.getIncludeNull();
                        List<ResaleShopResponBean> list = new ArrayList<>();
                       for(WaterShopResponBean bean:tempList){
                           ResaleShopResponBean resaleShopResponBean = new ResaleShopResponBean();
                           resaleShopResponBean.setGoods_title(bean.getGoods_title());
                           resaleShopResponBean.setGoods_img(bean.getGoods_img());
                           list.add(resaleShopResponBean);
                       }
                        if (mView != null)
                            mView.showContent(list);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null)
                            mView.showContentError();
                    }
                })
        );
    }

    @Override
    public void getJfgoodscate() {
        //获取分类
        addSubscribe(mDataManager.getJfgoodscate()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<WaterGoodsClassifyBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<WaterGoodsClassifyBean>> optional) {
                        if (mView != null)
                            mView.showJfgoodscate(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }
}
